package com.example.lycdays.ui.index;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.lycdays.MainActivity;
import com.example.lycdays.R;
import com.example.lycdays.data.bean.Event;
import com.example.lycdays.data.repositiory.EventDataBase;
import com.example.lycdays.ui.add.AddActivity;

import org.litepal.LitePal;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class IndexFragment extends Fragment {

    private final static String TAG = "myTag";

    RecyclerView recyclerView;
    IndexAdapter indexAdapter;

    Button button;

    private List<Event> events;

    public IndexFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_index, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        LitePal.getDatabase();

        recyclerView = getView().findViewById(R.id.recyclerView);
        indexAdapter = new IndexAdapter();

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));//线性的
        recyclerView.setAdapter(indexAdapter);
        events = EventDataBase.findAllEvents();
        //Log.d(TAG, "123: " + String.valueOf(events.get(0).getEventName()));
        indexAdapter.setAllEvents(events);
        indexAdapter.notifyDataSetChanged();

        ImageButton buttonAdd = getView().findViewById(R.id.buttonAdd);
        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity() , AddActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        events = EventDataBase.findAllEvents();
        //Log.d(TAG, "123: " + String.valueOf(events.get(0).getEventName()));
        indexAdapter.setAllEvents(events);
        indexAdapter.notifyDataSetChanged();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

}
